package com.gimi.cloud.bbp.manage.controller.sys;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.utils.ReflectUtils;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.dto.sys.SysAuthorityDTO;
import com.gimi.cloud.bbp.dto.sys.SysMenuDTO;
import com.gimi.cloud.bbp.manage.service.sys.SysAuthorityService;
import com.gimi.cloud.bbp.manage.service.sys.SysMenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 权限相关Controller
 *
 * @author wbj
 * @version 1.0
 * @Description
 * @create 2018-01-30
 **/
@RestController
@RequestMapping("/sys/auth")
public class SysAuthorityController {
    private static final Logger logger = LoggerFactory.getLogger(SysAuthorityController.class);

    private SysAuthorityService sysAuthorityService;
    private SysMenuService sysMenuService;

    @Autowired
    public SysAuthorityController(SysAuthorityService sysAuthorityService, SysMenuService sysMenuService) {
        this.sysAuthorityService = sysAuthorityService;
        this.sysMenuService = sysMenuService;
    }

    /**
     * 根据角色id获取角色权限信息
     * @param request
     * @param response
     * @param sysAuthorityDTO
     * @throws Exception
     */
    @RequestMapping("/roleMenus")
    public Response roleMenus(HttpServletRequest request, HttpServletResponse response, @RequestBody  SysAuthorityDTO sysAuthorityDTO)throws  Exception{
        Response resp = new Response();
        SysMenuDTO sysMenuDTO = new SysMenuDTO();
        ReflectUtils.copyPropertiesIgnoreNullAndEmptyStr(sysAuthorityDTO,sysMenuDTO);
        List<SysMenuDTO> menuList = sysMenuService.getMenuByRoId(sysMenuDTO,sysAuthorityDTO.getRoId());
        List<Integer> checked = new ArrayList<>();
        for(SysMenuDTO menuDto:menuList){
            if(null != menuDto.getHasAuth() && 1 == menuDto.getHasAuth()){
                checked.add(menuDto.getMeId());
            }
        }
        Map<String,Object> resData = new HashMap<>();
        resData.put("menus",sysMenuService.menuToTree(menuList));
        resData.put("checked",checked);
        resp.setRes_data(resData);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        return resp;
    }

    @RequestMapping("/doAuth")
    public Response doAuth(HttpServletRequest request,HttpServletResponse response,@RequestBody  SysAuthorityDTO sysAuthorityDTO) throws  Exception{
        Response resp = new Response();
        sysAuthorityService.doAuth(sysAuthorityDTO,sysAuthorityDTO.getRoId(),sysAuthorityDTO.getMeIds());
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"授权成功");
        return resp;
    }


}
